package com.iris.utils;

import java.io.File;
import java.io.IOException;

public class ClassCopy {

	/**
	 * 说明: <br>
	 * @param javaDir java的SRC文件目录
	 * @param classDir class目录
	 * @param outputDir 输出目录
	 * @throws IOException<br>
	 */
	public void copyClassFileByJavaFileDir(String javaDir,String classDir,String outputDir) throws IOException{
		File javaFileDir=new File(javaDir);
		copyClassFileByJavaFileDir(javaFileDir, classDir, outputDir,"");
	}
	private void copyClassFileByJavaFileDir(File javaFile,String classDir,String outputDir,String relativePath) throws IOException{
//		File classFileDir=new File(classDir);
		File[] javaRootFiles = javaFile.listFiles();
		String fileName="";//读取到的文件名
		String simpleFileName="";//没有后缀的文件名
		String classPath="";//要拷贝的class目录
		for (File file : javaRootFiles) {
			if(file.isFile()){
				fileName=file.getName();
				if(fileName.endsWith(".java")){
					simpleFileName=fileName.substring(0,fileName.indexOf("."));
					System.out.println(outputDir+""+relativePath+simpleFileName+".class");
					try {
						FilesUtil.copyFile(new File(classDir+relativePath+simpleFileName+".class"), new File(outputDir+""+relativePath+simpleFileName+".class"));
					} catch (Exception e) {
						System.out.println("没有找到class=================="+classDir+relativePath+simpleFileName+".class");
					}
					classPath=classDir+""+relativePath;//要拷贝的class路径
					File[] classPathFiles=new File(classPath).listFiles();
					for (File file2 : classPathFiles) {
						if(file2.getName().startsWith(simpleFileName+"$")&&file2.getName().endsWith(".class")){

							System.out.println(outputDir+""+relativePath+file2.getName());
							FilesUtil.copyFile(file2, new File(outputDir+""+relativePath+file2.getName()));
						}
					}
					
				}else{
					FilesUtil.copyFile(file, new File(outputDir+""+relativePath+file.getName()));
					System.out.println(outputDir+""+relativePath+file.getName());
				}
			}else if(file.isDirectory()){
				copyClassFileByJavaFileDir(file, classDir, outputDir, relativePath+"/"+file.getName()+"/");
			}
		}
		
	}
	
	public static void main(String[] args) {
		ClassCopy cc=new ClassCopy();
		try {
//			cc.copyClassFileByJavaFileDir("D:/code/versionCode/1110/src", "D:/code/pactera_manager/WebRoot/WEB-INF/classes", "D:/code/versionCode/1110");

			cc.copyClassFileByJavaFileDir("E:/export/src/main/java", "E:/work/zed_player_boss/target/classes", "E:/export/src/main/");
//			cc.copyClassFileByJavaFileDir("E:/export/src/main/java", "E:/work/zed_player_cloud_http_api/target/classes", "E:/export/src/main/");
//			cc.copyClassFileByJavaFileDir("E:/export/src/main/java", "E:/work/zed_player_cloud_http_api/WebContent/WEB-INF/classes", "E:/export/src/main/");
//			cc.copyClassFileByJavaFileDir("G:\\CMB\\CORE\\TRUNK\\cmb_mds\\src", 
//					"D:\\new_code\\cmb_mds\\WebContent\\WEB-INF\\classes", 
//					"G:\\CMB\\CORE\\TRUNK\\cmb_mds");
			
//			cc.copyClassFileByJavaFileDir("D:/code/versionCode/etlmapping/src", "D:/code/pactera_etlmapping_file_mm/bin", "D:/code/versionCode/etlmapping");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
